<?php

namespace Admin\Controller;

use Think\Controllers;
echojjidjfijdfdfssssss
/**////
 * 类功能描述:授权管理,包括:角色,功能,权限设置
 */
class UserController extends CommonController {

    /**
     * 管理员列表
     */

    public $pagesize = 15;

    public function index() {

        $this->display(); // 输出模板
    }

    public function AddUser() {
        $this->display();
    }
    public function editcompany(){
        $id = I("id", 0, int);
        $company = '';
        $addr = '';
        $tel = '';
        $legal = '';
        $phone = '';
        $code = '';
        $src='';
        if ($id != 0) {
            $mydb = M("wr_project_company");
            $where['id'] = $id;
            $myrs = $mydb->where($where)->find();
            $company = $myrs['company_name'];
            $addr = $myrs['company_address'];
            $tel = $myrs['company_tel'];
            $legal = $myrs['company_legal'];
            $phone = $myrs['phone'];
            $code = $myrs['company_snid'];
            $src=$myrs['img_src'];
        }
        // $db = M("wr_auth_rules");
        // $rs = $db->order("weight asc")->select();
        // $this->assign("rules_rs", $rs);
        $this->assign("company", $company);
        $this->assign("addr", $addr);
        $this->assign("tel", $tel);
        $this->assign("legal", $legal);
        $this->assign("phone", $phone);
        $this->assign("code", $code);
        $this->assign('src',$src);
        $this->assign("id", $id);
        $this->display();

    }

    /**
    *公司管理
    */
    public function checkCompany() {
        $page = I("page", 1, int); //接收当前记录页码
        $query_str = I("q");
        $paramer = "";
        $pgcount = 0;
        if (isset($query_str)) {
            if (!empty($query_str)) {
                $where['_string'] = '(company_name like "%' . $query_str . '%" or company_tel like "%' . $query_str . '%" or company_legal like "%' . $query_str . '%")';
                $paramer = "&q=" . $query_str;
            } else {
                $where = array();
            }
        }
        $db = M("wr_project_company");
        $fieldstr = 'id,company_name,company_address,company_code,company_tel,company_legal,phone,company_snid,dtime,is_checked,img_src';
        $limitfield = ($page - 1) * $this->pagesize . "," . $this->pagesize;
        $listdata = $db->field($fieldstr)->where($where)->order("id desc")->limit($limitfield)->select();
        $count = $db->where($where)->count();
        //计算分页总数
        if ($count > 0) {
            $pgcount = ceil($count / $this->pagesize);
        }
        // print_r($pgcount);
        $this->assign("rs", $listdata);
        $pagelist = $this->page_list_memu($pgcount, $page, 3, U("Admin/User/checkCompany"), $paramer, $count);
        $this->assign("pagelist", $pagelist);
        $this->assign("page", $page);
        $this->assign("q", $query_str);
        $ndata = '没有数据';
        $this->assign("nodata", $ndata);
        $this->display();
    }
    /**
    *新增公司信息提交
    */
    public function ajaxAddCompany(){
        $company=I('company','','htmlspecialchars');
        $addr=I('addr','','htmlspecialchars');
        $tel=I('tel');
        $legal=I('legal','','htmlspecialchars');
        $phone=I('phone');
        $code=I('code','','htmlspecialchars');
        $id=I('id',0,int);
        $src=I('src','','htmlspecialchars');
        if($id==0){
            $img=$this->uploadImg($_FILES['upfile']);
            if ($img=='error') {
                echo json_encode(array('status'=>'error','msg'=>'图片上传失败'));
                return ;
            }else{
            $companyCode=$this->CreateCompanyCode();
            $data=array('company_code'=>$companyCode,'company_name'=>$company,'company_address'=>$addr,'company_tel'=>$tel,

                    'company_legal'=>$legal,'phone'=>$phone,'company_snid'=>$code,'dtime'=>time(),'is_checked'=>0,'img_src'=>$img
                );

            $result=M('wr_project_company')->add($data);
            }

        }else{
            if (empty($_FILES['upfile'])) {
                $data=array('company_name'=>$company,'company_address'=>$addr,'company_tel'=>$tel,
                            'company_legal'=>$legal,'phone'=>$phone,'company_snid'=>$code
                            );
                $result=M('wr_project_company')->where(array('id'=>$id))->save($data);
            }else{
                $img=$this->uploadImg($_FILES['upfile']);
                if ($img=='error') {
                        echo json_encode(array('status'=>'error','msg'=>'图片上传失败'));
                        return ;
                }else{
                    $data=array('company_name'=>$company,'company_address'=>$addr,'company_tel'=>$tel,
                            'company_legal'=>$legal,'phone'=>$phone,'company_snid'=>$code,'img_src'=>$img
                        );
                    $result=M('wr_project_company')->where(array('id'=>$id))->save($data);
                     unlink($_SERVER["DOCUMENT_ROOT"].$src);
                    }
            }

        }

        if($result){

            echo json_encode(array('status'=>'success','msg'=>'保存成功'));
        }else{
            echo json_encode(array('status'=>'error','msg'=>'保存失败'));
        }

    }

     public function CheckCompanyStatu() {
        $id = I("id", 0, int);
        $v = I("setval", 0, int);
        $check = $v == 0 ? 1 : 0;
        $db = M("wr_project_company");
        $where['id'] = array('eq', $id);
        $data = array('is_checked' => $check);
        $back = $db->where($where)->save($data);
        if ($back) {
            $this->ajaxReturn(array('status' => 'success', 'val' => $check));
        } else {
            $this->ajaxReturn(array('status' => 'error'));
        }

    }


    /**
    *数据删除
    */
    public function delNews(){
        $id=I('id',0,int);
        $img_src=I('img_src','','htmlspecialchars');
        if($id==0){
            echo json_encode(array('status'=>'error'));
        }else{
            $result=M('wr_project_company')->where(array('id'=>$id))->delete();
            if ($result){
                 unlink($_SERVER["DOCUMENT_ROOT"].__APP__.'/'.$img_src);
                 echo json_encode(array('status'=>'success','img_src'=>$_SERVER["DOCUMENT_ROOT"].__APP__.'/'.$img_src));
             }else{
                echo json_encode(array('status'=>'error','img_src'=>$_SERVER["DOCUMENT_ROOT"].__APP__.'/'.$img_src));
             }


        }
    }
public function swi(){
    echo 'hello';
}

      /**
    *数据批量删除
    */
    public function delMoreNews(){
        $ids=I('ids','',"htmlspecialchars");
        $srcs=I('img_srcs','','htmlspecialchars');
        $src=explode(',', $srcs);
        if ($ids!='') {
           $result=M('wr_project_company')->delete($ids);
           if ($result) {
                for($i=0;$i<count($src);$i++){
                    $cut=strstr($src[$i], 'Uploads/');
                   unlink($_SERVER["DOCUMENT_ROOT"].__APP__.'/'.$cut);                }
               echo json_encode(array('status'=>'success','msg'=>'数据删除成功'));
           }else{
                echo json_encode(array('status'=>'error','msg'=>'数据删除失败'));
           }
        }

    }

    /**
    *图片上传
    */
    public function uploadImg($file){
        $name = $file['name'];
        $type=pathinfo($name, PATHINFO_EXTENSION);
        // $type = strtolower(substr($name,strrpos($name,'.')+1)); 
        $allow_type = array('jpg','jpeg','png');   
        if(!in_array($type, $allow_type)){     
       
       
          return 'error';
        }
        $upload_path ="Uploads/image/company/";
        if(!is_dir($upload_path)){
            mkdir('Uploads/image/company/',0777,true);
        }
        $fileName=time().rand(100000,9999999).'.'.$type;
        if(move_uploaded_file($file['tmp_name'],$upload_path.$fileName)){
            return $upload_path.$fileName;

        }else{
          return 'error';

        }
    }

    
}
